package isd.com.smarthome;


import isd.com.smarthome.adapter.HomeAdapter;
import isd.com.smarthome.login.LoginDatabase;
import isd.com.smarthome.model.Query;
import isd.com.smarthome.model.Room;
import isd.com.smarthome.provider.SmartHomeProvider;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TitlesFragment extends BaseFragment {

	private ExpandableListView exp_listFloor;
	private ImageView imv_system;
	private ImageView imv_user;
	private Query query;
	private String user="admin";
	private String pass ="admin";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View rootView = inflater.inflate(R.layout.titlesfragment, container,false);
		exp_listFloor = (ExpandableListView) rootView.findViewById(R.id.exp_listfloor);
		imv_system = (ImageView) rootView.findViewById(R.id.imv_system);
		imv_user = (ImageView) rootView.findViewById(R.id.imv_user);
		
		
			setView(rootView);
		
		return rootView;
	}

	public void setView(View view) {
		
		Cursor data = getActivity().getContentResolver().query(
				SmartHomeProvider.FLOOR_CONTENT_URI, null, null, null, null);
		ListAdapter adapter = exp_listFloor.getAdapter();
		if (adapter == null) {
			HomeAdapter homeadapter = new HomeAdapter(data, getActivity());
			exp_listFloor.setAdapter(homeadapter);
		} else {
			HomeAdapter homeadapter = (HomeAdapter) adapter;
			homeadapter.setGroupCursor(data);
			homeadapter.notifyDataSetChanged();
		}
		
		// serch in to Detail

		exp_listFloor.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Room room = (Room) v.getTag();

				DetailsFragment frag = new DetailsFragment();
				// DetailsFragment.bt_chon.setVisibility(View.VISIBLE);
				frag.setRoom(room);
				frag.setQuery(query);
				android.app.FragmentTransaction transaction = getFragmentManager()
						.beginTransaction();
				transaction.replace(R.id.details_frag, frag);
				transaction.addToBackStack(null);
				transaction.commit();

				return false;
			}

		});
		imv_system.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				OnclickLogin();
			}
		});
		imv_user.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
	}


	public void OnclickLogin() {
		// check Dialog
	
				// TODO Auto-generated method stub
				final Context context = getActivity();
				LayoutInflater li = LayoutInflater.from(context);
				View prompt = li.inflate(R.layout.login, null);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						context);
				alertDialogBuilder.setView(prompt);				
				final EditText ed_name = (EditText) prompt.findViewById(R.id.ed_name);
				final TextView ed_pass = (TextView) prompt.findViewById(R.id.ed_pass);
				alertDialogBuilder.setTitle("Admin Login Acc and Pass");
				alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								String password = ed_pass.getText().toString();
								String username = ed_name.getText().toString();
								
								if(user.equalsIgnoreCase(username) && pass.equalsIgnoreCase(password)){
									//chuyen sang activity khac 
									LoginDatabase frag = new LoginDatabase();
									// DetailsFragment.bt_chon.setVisibility(View.VISIBLE);
									android.app.FragmentTransaction transaction = getFragmentManager()
											.beginTransaction();
									transaction.replace(R.id.details_frag, frag);
									transaction.addToBackStack(null);
									transaction.commit();
								}else{
									Toast.makeText(context,"user hay pass cua ban khong hop le", Toast.LENGTH_LONG).show();
								}
								

							}
						});

				alertDialogBuilder.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();

							}
						});
				alertDialogBuilder.show();

			
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

}
